/**
 *
 * Copyright 2003-2011 Simple Logo Viewer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.logo.viewer.logo.graphic;

import java.util.Observable;
import java.util.Observer;

import org.logo.viewer.LogoManager;
import org.logo.viewer.logo.graphic.frontend.LogoInterface;

/**
 * Interfaccia grafica di LOGO
 * 
 * @author Marco Speranza
 * @version 0.1
 */
public class Graphic implements Observer {

	/**
	 * Composizione con L'interfaccia Grafica (Frame)
	 */
	private LogoInterface f;

	/**
	 * Associazione Con LogoManager
	 */
	private LogoManager logoManager;

	/**
	 * Creates a new instance of Graphic
	 * 
	 * @param logoManager
	 *            associazione con logo Manager
	 */
	public Graphic(LogoManager logoManager) {
		this.makeInterface(logoManager);
	}

	/**
	 * Implementazione del metodo di observer
	 * 
	 * @param codaStampa
	 *            Classe Observable e' la cosadi stampa (PATTERN SINGLETON)
	 * @param param
	 *            Parametri
	 */
	public synchronized void update(Observable codaStampa, Object param) {
		// if (f.isIdle() && f.isDrawable()) {
		if (f.isIdle()) {
			f.updateFigure((Azione) param);
		}
	}

	/**
	 * Costruisce l'interfaccia grafica e setta le associazioni e lancia il
	 * thread grafico
	 * 
	 * @param logoManager
	 *            Associazione con la classeLogoManager
	 */
	private void makeInterface(LogoManager logoManager) {
		this.f = new LogoInterface(this);
		this.logoManager = logoManager;
		// new Thread(f).run();
		new Thread(f).start();
	}

	/**
	 * Start del programma
	 * 
	 * @throws Exception
	 */
	public void startProgram() throws Exception {
		logoManager.start();
	}

	/**
	 * Stop del programma
	 */
	public void stopProgram() {
		logoManager.stop();
	}

	/**
	 * Start del programma
	 */
	public void endProgram() {
		f.endProgram();
	}

	/**
	 * @param program
	 * @throws Exception
	 */
	public void loadProgram(String program) throws Exception {
		logoManager.loadProgram(program);
	}

}
